package com.thb.base.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.thb.base.model.Community;
import com.thb.base.model.Department;
import com.thb.base.model.Label;
import com.thb.base.service.IAcadeService;
import com.thb.base.service.IDepartmentService;
import com.thb.base.service.ILabelService;
import com.thb.exception.ExceptionCodeUtil;
import com.thb.exception.ServiceException;
import com.thb.logs.utils.LogUtils;
import com.thb.utils.ResponseTool;

@Controller
public class AcademyController {
	
	private static final Logger logger = LogUtils.getBusinessLog();
	
	@Autowired
	private IAcadeService acadeService;
	
	@Autowired
	private IDepartmentService departmentService;
	
	@Autowired
	private ILabelService labelService;
	
	@RequestMapping(value = "/acade/list" , method = RequestMethod.POST)
	@ResponseBody
	public Object list(HttpServletRequest request){
		String json = request.getParameter("args");
		logger.info(String.format("args is: %s", json));

		List<Community> jList = acadeService.findAllIn();
		
		return ResponseTool.returns(jList);
	}
	
	@RequestMapping(value = "/department/list" , method = RequestMethod.POST)
	@ResponseBody
	public Object listDepartment(HttpServletRequest request){
		String json = request.getParameter("args");
		logger.info(String.format("args is: %s", json));
		
		JSONObject obj = JSONObject.parseObject(json);
		String communityId = obj.getString("communityId");
		
		if(communityId == null || communityId.equals("")){
			throw new ServiceException(ExceptionCodeUtil.CommonErrorCode, "参数不能为空");
		}

		List<Department> jList = departmentService.findByCommunityId(communityId);
		
		return ResponseTool.returns(jList);
	}
	
	@RequestMapping(value = "/posters/listAllAcade" , method = RequestMethod.POST)
	@ResponseBody
	public Object join(HttpServletRequest request){
		String json = request.getParameter("args");
		logger.info(String.format("args is: %s", json));
//		JSONObject jo = JSONObject.parseObject(json);
		
		List<Community> in = acadeService.findAllIn();
		List<Community> out = acadeService.findAllOut();
		
		JSONObject o = new JSONObject();
		o.put("in", in);
		o.put("out", out);
		
		return ResponseTool.returns(o);
	}
	
	@RequestMapping(value = "/posters/labelList" , method = RequestMethod.POST)
	@ResponseBody
	public Object labelList(HttpServletRequest request){
		String json = request.getParameter("args");
		logger.info(String.format("args is: %s", json));
//		JSONObject jo = JSONObject.parseObject(json);
		
		List<Label> lList = labelService.findAll();
		
		return ResponseTool.returns(lList);
	}

}
